﻿
using System;
using System.IO;

namespace TextPreprocessing
{
    
    public class Program
    {
        /** Test program for demonstrating the Stemmer.  It reads text from a
         * a list of files, stems each word, and writes the result to standard
         * output. Note that the word stemmed is expected to be in lower case:
         * forcing lower case must be done outside the Stemmer class.
         * Usage: Stemmer file-name file-name ...
         */
        private static string nonspamTrainFolderPath = "nonbot-train";
        private static string spamTrainFolderPath = "bot-train";
        private static string spamTestFolderPath = "spam-test";
        private static string nonspamTestFolderPath = "nonspam-test";

        private static string nonspamTrainFolderPreprocessPath = "nonbot-train-preprocessed";
        private static string spamTrainFolderPreprocessPath = "bot-train-preprocessed";
        private static string spamTestFolderPreprocessPath = "spam-test-preprocessed";
        private static string nonspamTestFolderPreprocessPath = "nonspam-test-preprocessed";

        public static void Main(String[] args)
        {
            Console.WriteLine(1);
            PreprocessFiles(nonspamTrainFolderPath, nonspamTrainFolderPreprocessPath);
            Console.WriteLine(2);
            PreprocessFiles(spamTrainFolderPath, spamTrainFolderPreprocessPath);
            //Console.WriteLine(3);
            //PreprocessFiles(spamTestFolderPath, spamTestFolderPreprocessPath);
            //Console.WriteLine(4);
            //PreprocessFiles(nonspamTestFolderPath, nonspamTestFolderPreprocessPath);
        }

        public static void PreprocessFiles(string inDirectory, string outDirectory)
        {
            foreach (string file in Directory.EnumerateFiles(inDirectory))
            {
                string text = File.ReadAllText(file);
                text = StopWordRemover.CleanSearchedWords(text);
                string[] words = text.Split(' ');
                PorterStemmer s = new PorterStemmer();
                text = "";
                foreach (string word in words)
                {
                    s.add(word.ToCharArray(), word.Length);
                    s.stem();
                    string x = s.ToString();
                    text += x + " ";
                }
                string name = file.Split('\\')[1];
                string path = outDirectory + "\\" + name;
                System.IO.File.WriteAllText(path, text);
            }
        }
    }
}